package com.blackcat.demo.sm;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.SM2;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;

import java.util.Base64;

/**
 * 描述 : 国密sm3
 *
 * @author : zhangdahui  2024/5/15 下午2:08
 * @version 1.0
 */
public class SM3Dome {

    public static void main(String[] args) {
        SM2 sm2 = SmUtil.sm2();
        String privateKey = HexUtil.encodeHexStr(BCUtil.encodeECPrivateKey(sm2.getPrivateKey()));
        System.out.println("私钥 " + privateKey);

        String password = "123456.123";

        String encryptStr = encrypt(privateKey, password);
        System.out.println("加密 " + encryptStr);
    }

    /**
     * 加密
     *
     * @param key 秘钥
     * @param data 明文
     * @return 密文
     */
    public static String encrypt(String key, String data) {
        byte[] bytes = SmUtil.hmacSm3(Base64.getDecoder().decode(key))
                .digest(data);
        return Base64.getEncoder().encodeToString(bytes);
    }

}
